import os


def pause():
    """按任意键继续"""
    os.system("pause > nul")


class Option:
    def __init__(self, text: str, next: str) -> None:
        self.text = text
        self.next = next


class Page:
    def __init__(
        self, name: str, title: str, content: list, options: list
    ) -> None:
        self.name = name
        self.title = title
        self.content = content
        self.options = options

    def run(self) -> str:
        print("---------------------")
        print(self.title)
        print("---------------------")
        for text in self.content:
            print(text)
            pause()

        print("\n请选择：")
        options_len = len(self.options)
        for i in range(options_len):
            print("%d:\t%s" % (i + 1, self.options[i].text))

        while True:
            try:
                i = int(input("请输入序号（1~%d）：" % (options_len)))
                if i > 0 and i <= options_len:
                    print()
                    return self.options[i - 1].next
            except:
                pass


pages = [
    Page(
        "page1",
        "序幕",
        ["你好", "介绍一下", "……", "你会怎么选？"],
        [Option("选项一", "page2"), Option("选项二", "page3"), Option("选项三", "page4")],
    ),
    Page(
        "page2",
        "第一章",
        ["你好", "介绍一下", "……", "你会怎么选？"],
        [Option("选项一", "page1"), Option("选项二", "page3"), Option("选项三", "page4")],
    ),
    Page(
        "page3",
        "第二章",
        ["你好", "介绍一下", "……", "你会怎么选？"],
        [Option("选项一", "page1"), Option("选项二", "page2"), Option("选项三", "page5")],
    ),
    Page(
        "page4",
        "第三章",
        ["你好", "介绍一下", "……", "你会怎么选？"],
        [Option("选项一", "page2"), Option("选项二", "page3"), Option("选项三", "page5")],
    ),
    Page(
        "page5",
        "尾声",
        ["再见", "……", "你会怎么选？"],
        [Option("选项一", "page3"), Option("选项二", "page4"), Option("结束", "end")],
    ),
]

index = dict()
for page in pages:
    index[page.name] = page

next = pages[0].name
while next != "end":
    page = index[next]
    if page is not None:
        next = index[next].run()
    else:
        print("发生异常：找不到名称为 %s 的游戏页面。" % next)
        break

print("游戏结束")
